import requests as rq

class LimsReqUtil:

    # 在__init__方法外，是类成员变量，用类名就可以调用，也可以用self调用
    # baseurl = 'http://localhost:8001/LIMS-Server/MPLM_Service_Gateway/'
    def __init__(self):
        # 实例成员变量，只能用self调用
        self.baseurl = 'http://localhost:8001/LIMS-Server/MPLM_Service_Gateway/'

    # 成员函数，第一个参数是 self,只是一个硬性的规定
    def getToken(self):
        url = self.baseurl+'TokenGenerator/getToken'
        headers = {
            '__TokenAuthorization_UserName_': 'ex_cuiml1',
            '__TokenAuthorization_Function_': 'getTestForm'
        }
        response = rq.post(url, headers=headers)
        # print(response.text)
        # print(response.json())
        retjson = response.json()
        # print(retjson['token'])
        return retjson['token']

    def getTestForm(self):
        url = 'raclimsRestfulController/getTestForm'
        data = {
            'EntrustmentOrder': 'ST2023040019'
        }

        # 调用成员函数和成员变量，用self
        return self.postForm(url=url,data=data)

    def postForm(self, url, data):
        # 调用成员变量
        url = self.baseurl+url
        token = self.getToken()
        headers = {
            '__TokenAuthorization_UserName_': 'ex_cuiml1',
            '__TokenAuthorization_Function_': 'getTestForm',
            '__TokenAuthorization_UID_': token
        }
        response = rq.post(url=url, headers=headers, data=data)
        return response.json()

if __name__ == '__main__':
    # 创建对象
    util = LimsReqUtil()
    # 请求
    json = util.getTestForm()
    print(json)

    # 调用类属性
    # print(LimsReqUtil.baseurl) 
   
    # 调用实例属性
    # print(util.baseurl)
